﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using OfficeOpenXml;
using OfficeOpenXml.Style;

namespace shop.Web.Excel
{
    public static class EpplusExtention
    {


        public static void SetRowHeight(this ExcelRange range, double height)
        {
            for (int i = range.Start.Row; i <= range.End.Row; i++)
            {
                range.Worksheet.Row(i).Height = height;
            }
        }


        public static void SetAllBorder(this Border border, ExcelBorderStyle borderStyle )
        {
            border.Left.Style = borderStyle;
            border.Right.Style = borderStyle;
            border.Top.Style = borderStyle;
            border.Bottom.Style = borderStyle;
        }



        ///// <summary> 设置字体 部分设置没有添加进来 </summary>
        //public static void SetFont(ExcelRange rng, Font font)
        //{
        //    rng.Style.Font.Bold = font.Bold;
        //    rng.Style.Font.Name = font.Name;
        //    rng.Style.Font.Italic = font.Italic;
        //    rng.Style.Font.Size = font.Size;
        //    rng.Style.Font.UnderLine = font.Underline;
        //    //rng.Style.Font.VerticalAlign = font.GdiVerticalFont;
        //}

        ///// <summary> 设置字体, 部分设置没有添加进来 </summary>
        //public static void SetFont(ExcelRange rng, ExcelFont font)
        //{
        //    rng.Style.Font.Bold = font.Bold;
        //    rng.Style.Font.Name = font.Name;
        //    rng.Style.Font.Italic = font.Italic;
        //    rng.Style.Font.Size = font.Size;
        //    rng.Style.Font.UnderLine = font.UnderLine;
        //    rng.Style.Font.UnderLineType = font.UnderLineType;
        //    rng.Style.Font.Strike = font.Strike;
        //    rng.Style.Font.VerticalAlign = font.VerticalAlign;
        //    //rng.Style.Font.Color.SetColor(font.Color.Indexed);

        //    //rng.Style.Font.VerticalAlign = font.GdiVerticalFont;
        //}

        /// <summary> 设置格式 </summary>
        public static void SetMyStyle(this ExcelRange rng, CellStyleStuck style)
        {
            SetMyStyle(rng.Style, style);

            //if (style.Numberformat!= null) rng.Style.Numberformat.Format = style.Numberformat;
            //if (style.Font != null) rng.Style.Font.SetFromFont(style.Font);

            //rng.Style.Border.Bottom.Style = style.BorderBottom;
            //rng.Style.Border.Top.Style = style.BorderTop;
            //rng.Style.Border.Left.Style = style.BorderLeft;
            //rng.Style.Border.Right.Style = style.BorderRight;

            //rng.Style.HorizontalAlignment = style.HorizontalAlignment;
            //rng.Style.VerticalAlignment = style.VerticalAlignment;
            //rng.Style.WrapText = style.WrapText;
            //rng.Style.ReadingOrder = style.ReadingOrder;
            //rng.Style.ShrinkToFit = style.ShrinkToFit;
            //rng.Style.Indent = style.Indent;
            //rng.Style.TextRotation = style.TextRotation;
            //rng.Style.Locked = style.Locked;
            //rng.Style.Hidden = style.Hidden;
            //rng.Style.QuotePrefix = style.QuotePrefix;
            //rng.Style.XfId = style.XfId;
            //rng.Style.Fill = style.Fill;
            
        }

        /// <summary> 设置格式 </summary>
        public static void SetMyStyle(this ExcelStyle rngStyle, CellStyleStuck style)
        {
            if (style.Numberformat != null) rngStyle.Numberformat.Format = style.Numberformat;

//#if NET45
            if (style.Font != null) rngStyle.Font.SetFromFont(style.Font.Name, style.Font.Size, style.Font.Bold, style.Font.Italic, style.Font.Underline, style.Font.Strikeout);
//#else
//            if (style.Font != null) rngStyle.Font.SetFromFont(style.Font);
//#endif

            if (style.Font != null) rngStyle.Font.Bold =(style.Font.Bold);
            rngStyle.Border.Bottom.Style = style.BorderBottom;
            rngStyle.Border.Top.Style = style.BorderTop;
            rngStyle.Border.Left.Style = style.BorderLeft;
            rngStyle.Border.Right.Style = style.BorderRight;

            rngStyle.HorizontalAlignment = style.HorizontalAlignment;
            rngStyle.VerticalAlignment = style.VerticalAlignment;
            rngStyle.WrapText = style.WrapText;
            rngStyle.ReadingOrder = style.ReadingOrder;
            rngStyle.ShrinkToFit = style.ShrinkToFit;
            rngStyle.Indent = style.Indent;
            rngStyle.TextRotation = style.TextRotation;
            rngStyle.Locked = style.Locked;
            rngStyle.Hidden = style.Hidden;
            rngStyle.QuotePrefix = style.QuotePrefix;
            rngStyle.XfId = style.XfId;

            if (style.BackColor != Color.Empty)
            {
                rngStyle.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
                rngStyle.Fill.BackgroundColor.SetColor(style.BackColor);
            }
        }


        /// <summary>
        /// Workbook 导出到文件, 扩展方法
        /// </summary>
        /// <param name="pck"></param>
        /// <param name="strFileName"></param>
        /// <returns>返回文件路径</returns>
        public static string SaveExcelFile(this ExcelPackage pck, string strFileName)
        {
            if (pck == null)
                return "";

            //得到一个文件名
            //strFileName = ExcelHelper.GetExcelName(strFileName);
            if (string.IsNullOrEmpty(strFileName))
                return "";

            FileInfo fi = new FileInfo(strFileName);
            pck.SaveAs(fi);
            
 
            //Program.RecentFiles.AddHistory(strFileName);
            return strFileName;
        }




    }

    /// <summary> 记录单元格样式的结构 </summary>
    public class CellStyleStuck
    {
        //
        // 摘要:
        //     Numberformat
        public string Numberformat { get; set; }
        //
        // 摘要:
        //     Font styling
        public Font Font { get; set; }


        /// <summary> 背景色 </summary>
        public Color BackColor { set; get; }

        ////     Fill Styling
        //public ExcelFill Fill { get; set; }

        //
        // 摘要:
        //     Border
        public ExcelBorderStyle BorderBottom { get; set; } = ExcelBorderStyle.Thin;
        public ExcelBorderStyle BorderLeft { get; set; } = ExcelBorderStyle.Thin;
        public ExcelBorderStyle BorderTop { get; set; } = ExcelBorderStyle.Thin;
        public ExcelBorderStyle BorderRight { get; set; } = ExcelBorderStyle.Thin;

        //
        // 摘要:
        //     The horizontal alignment in the cell
        public ExcelHorizontalAlignment HorizontalAlignment { get; set; }
        //
        // 摘要:
        //     The vertical alignment in the cell
        public ExcelVerticalAlignment VerticalAlignment { get; set; }
        //
        // 摘要:
        //     Wrap the text
        public bool WrapText { get; set; }
        //
        // 摘要:
        //     Readingorder
        public ExcelReadingOrder ReadingOrder { get; set; }
        //
        // 摘要:
        //     Shrink the text to fit
        public bool ShrinkToFit { get; set; }
        //
        // 摘要:
        //     The margin between the border and the text
        public int Indent { get; set; }
        //
        // 摘要:
        //     Text orientation in degrees. Values range from 0 to 180.
        public int TextRotation { get; set; }
        //
        // 摘要:
        //     If true the cell is locked for editing when the sheet is protected OfficeOpenXml.ExcelWorksheet.Protection
        public bool Locked { get; set; }
        //
        // 摘要:
        //     If true the formula is hidden when the sheet is protected. OfficeOpenXml.ExcelWorksheet.Protection
        public bool Hidden { get; set; }
        //
        // 摘要:
        //     If true the cell has a quote prefix, which indicates the value of the cell is
        //     prefixed with a single quote.
        public bool QuotePrefix { get; set; }
        //
        // 摘要:
        //     The index in the style collection
        public int XfId { get; set; }
    }

}
